/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * leafget
 * Copyright (C) xinliGG 2008 <xinliGG@gmail.com>
 * 
 * leafget is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * leafget is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <gtkmm/stock.h>

#include "new.h"


#define SIZE_X 500
#define SIZE_Y 400


New::New ( Gtk::Window &win, const LEAF_CONFIG_DATA &config )
	: Gtk::Dialog ( "New Task", win, false, false ),
	  defaultPass( config.defaultPass ),
	  boxFrameFile( false, 0 ),
	  boxURL( false, 0 ),
	  boxDir( false, 0 ),
	  boxName( false, 0 ),
	  boxRadio( false, 0 ),
	  LabelURL(  "  URL:          ", false ),
	  LabelDir(  "  Save To:    ", false ),
	  LabelName( "  Name:       ", false ),
	  LabelUser( "  User:         ", false ),
	  LabelPass( "  Pass:         ", false ),
	  Button_Folder ( "..." ),
	  RadioButtonOn( "On" ),
	  RadioButtonOff( "Off" ),
	  group ( RadioButtonOn.get_group() ),
	  AlignmentRadio( Gtk::ALIGN_CENTER, Gtk::ALIGN_CENTER, 0.0, 0.0 )
{
	
	newBox = get_vbox();
	newBox->set_homogeneous( false );
	newBox->set_spacing( 0 );

		/* File Options */
	
	FrameFile.set_label( "File Options" );
	FrameFile.set_label_align( Gtk::ALIGN_CENTER, Gtk::ALIGN_TOP );
	FrameFile.set_shadow_type( Gtk::SHADOW_ETCHED_OUT );	
	newBox->pack_start( FrameFile, Gtk::PACK_SHRINK, 3 );
	
//	EntryURL.set_width_chars( 50 );
//	EntryURL.set_activates_default( true );
	EntryURL.signal_changed().connect( sigc::mem_fun( *this, &New::on_changed_url ) );
	boxURL.pack_start( LabelURL, Gtk::PACK_SHRINK, 0 );
	boxURL.pack_start( EntryURL, Gtk::PACK_SHRINK, 0 );
	
	EntryDir.set_text( config.defaultPath );
//	EntryDir.set_width_chars( 47 );
//	EntryDir.set_activates_default( true );
	Button_Folder.signal_clicked().connect( sigc::mem_fun( *this, &New::on_button_folder_new ) );
	boxDir.pack_start( LabelDir, Gtk::PACK_SHRINK, 0 );
	boxDir.pack_start( EntryDir, Gtk::PACK_SHRINK, 0 );
	boxDir.pack_start( Button_Folder, Gtk::PACK_SHRINK, 0 );
	
//	EntryName.set_width_chars( 50 );
//	EntryName.set_activates_default( true );
	boxName.pack_start( LabelName, Gtk::PACK_SHRINK, 0 );
	boxName.pack_start( EntryName, Gtk::PACK_SHRINK, 0 );
	
	FrameFile.add( boxFrameFile );
	boxFrameFile.pack_start( boxURL, Gtk::PACK_SHRINK, 0 );
	boxFrameFile.pack_start( boxDir, Gtk::PACK_SHRINK, 0 );
	boxFrameFile.pack_start( boxName, Gtk::PACK_SHRINK, 0 );

		/* FTP Options */
/*
	FrameFTP.set_label( "FTP Options" );
	FrameFTP.set_label_align( Gtk::ALIGN_CENTER, Gtk::ALIGN_TOP );
	FrameFTP.set_shadow_type( Gtk::SHADOW_ETCHED_OUT );
	newBox->pack_start( FrameFTP, Gtk::PACK_SHRINK, 3 );

	EntryUser.set_text( "anonymous" );
	EntryUser.set_width_chars( 50 );
	EntryUser.set_activates_default( true );
	boxUser.pack_start( LabelUser, Gtk::PACK_SHRINK, 0 );
	boxUser.pack_start( EntryUser, Gtk::PACK_SHRINK, 0 );

	EntryPass.set_text( defaultPass );
	EntryPass.set_width_chars( 50 );
	EntryPass.set_activates_default( true );
	boxPass.pack_start( LabelPass, Gtk::PACK_SHRINK, 0 );
	boxPass.pack_start( EntryPass, Gtk::PACK_SHRINK, 0 );

	FrameFTP.add( boxFrameFTP );
	boxFrameFTP.pack_start( boxUser, Gtk::PACK_SHRINK, 0 );
	boxFrameFTP.pack_start( boxPass, Gtk::PACK_SHRINK, 0 );
*/
		/* Starting now? */
		
	FrameStartOptions.set_label( "Starting now?" );
	FrameStartOptions.set_label_align( Gtk::ALIGN_CENTER, Gtk::ALIGN_TOP );
	FrameStartOptions.set_shadow_type( Gtk::SHADOW_ETCHED_OUT );	
	newBox->pack_start( FrameStartOptions, Gtk::PACK_SHRINK, 3 );

	RadioButtonOff.set_group( group );
	AlignmentRadio.add( boxRadio );
	boxRadio.pack_start( RadioButtonOn, Gtk::PACK_SHRINK, 0 );
	boxRadio.pack_start( RadioButtonOff, Gtk::PACK_SHRINK, 0 );
	FrameStartOptions.add( AlignmentRadio );

	set_start( config.startNow );

		/* buttons */

	add_button( Gtk::Stock::CANCEL, 0 );
	add_button( Gtk::Stock::OK, 1 );

	set_default_response( 1 );	
	activate_default();

	show_all_children();	
}


	/* event functions */

void New::on_changed_url ( void )
{
	std::string url = EntryURL.get_text();

	unsigned int posA = url.find( "://" );

		/* 3 or 4 chars bebore :// ( ftp or http ) */
	if( posA != std::string::npos && ( posA == 3 || posA == 4 ) )
	{
		posA += 3;

			/* define file name */

		unsigned int posB = url.find_last_of( "/" );
		if ( posB != std::string::npos && posB > posA )
		{
			posB++;
			EntryName.set_text( url.substr( posB, url.length() - posB ) );
		}

			/* define ftp options */

		posB = url.find( "@", posA );
		if( posB != std::string::npos && posB > posA )
		{
			unsigned int posC = url.find( "/", posA );
			if( posC != std::string::npos && posC > posB )
			{
				posC = url.find( ":", posA );
				if( posC != std::string::npos && posC < posB && posC > posA )
				{
					EntryUser.set_text( url.substr( posA, posC - posA ) );
					posC++;
					EntryPass.set_text( url.substr( posC, posB - posC ) );
				}
			}
		}
		else
		{
			EntryUser.set_text( "anonymous" );
			EntryPass.set_text( defaultPass );			
		}
	}	
}

void New::on_button_folder_new ( void )
{
	Gtk::FileChooserDialog folderDialog( "Please choose a folder", Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER );
	folderDialog.set_transient_for( *this );

		/* add response buttons the the dialog */

	folderDialog.add_button( Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL );
	folderDialog.add_button( "Select", Gtk::RESPONSE_OK );

	int result = folderDialog.run();

		/* handle the response */

	if( result == Gtk::RESPONSE_OK )
		EntryDir.set_text( folderDialog.get_filename() );
}


	/* set functions */

void New::set_url ( const std::string &url )
{
	EntryURL.set_text( url );
	on_changed_url();
}

void New::set_start ( const bool & startNow )
{
	if( startNow )
		RadioButtonOn.set_active();
	else
		RadioButtonOff.set_active();		
}


	/* get functions */

const std::string New::get_url ( void ) const
{
	return EntryURL.get_text();
}

const std::string New::get_dir ( void ) const
{
	return EntryDir.get_text();
}

const std::string New::get_name ( void ) const
{
	return EntryName.get_text();
}

const std::string New::get_user ( void ) const
{
	return EntryUser.get_text();
}

const std::string New::get_pass ( void ) const
{
	return EntryPass.get_text();
}

const bool New::get_start ( void ) const
{
	return RadioButtonOn.get_active();
}


/* EOF */


